using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;

using miciv;
using micivCore.Player;

namespace micivCore.Faction
{
    public class Faction
    {
        protected IPlayer m_owner;
        public IPlayer Owner
        {
            get
            {
                return this.m_owner;
            }
            set
            {
                this.m_owner = value;
            }
        }

        protected string m_strID;
        public string ID
        {
            get
            {
                return this.m_strID;
            }
        }

        protected Faction m_parent;
        public Faction Parent
        {
            get
            {
                return this.m_parent;
            }
        }

        protected FactionHierarchy m_hierarchy;
        public FactionHierarchy Hierarchy
        {
            get
            {
                if (null == this.m_hierarchy)
                {
                    this.m_hierarchy = new FactionHierarchy(this);
                }
                return this.m_hierarchy;
            }
        }

        protected CoreContext m_coreContext;
        protected Hashtable m_htblGroups;

        public Faction(CoreContext _coreContext)
        {
            this.m_coreContext = _coreContext;
            this.m_strID = "UnnamanedFaction";
            this.m_htblGroups = new Hashtable();
        }

        public bool Initialize(XmlElement _xmlFaction)
        {
            this.m_strID = this.m_coreContext.CoreManager.GetXmlAttributeValue(_xmlFaction, "id", this.m_strID);
            string strParent = this.m_coreContext.CoreManager.GetXmlAttributeValue(_xmlFaction, "parent", string.Empty);
            this.m_parent = this.m_coreContext.GetData<FactionManager>("FactionManager").Get(strParent);
            return true;
        }

        public FactionGroup GetGroup(string _strID, bool _bCreateIsDoesntExist)
        {
            if ((false == this.m_htblGroups.ContainsKey(_strID)) && (false != _bCreateIsDoesntExist))
            {
                this.m_htblGroups.Add(_strID, new FactionGroup(this, _strID));
            }
            if (false != this.m_htblGroups.ContainsKey(_strID))
            {
                return this.m_htblGroups[_strID] as FactionGroup;
            }
            return null;
        }
    }
}
